using UnityEngine;

public class CourtStateQuarterEnd : AbstractCourtState
{
	public static CourtStateQuarterEnd instance;

	private CourtStateQuarterEnd()
	{
	}

	public static CourtStateQuarterEnd getInstance()
	{
		if (instance == null)
		{
			instance = new CourtStateQuarterEnd();
		}
		return instance;
	}

	public override void enter(Court owner)
	{
		base.enter(owner);
		owner.quarter_num++;
		if (owner.quarter_num == 3)
		{
			owner.teamUser.changeOffenseDirection();
			owner.teamComputer.changeOffenseDirection();
		}
		owner.teamUser.setIsAllAI(_isAllAI: true);
		for (int i = 0; i < Team.PlayerCount; i++)
		{
			owner.teamUser.getPlayer(i).AI.do_quarterend();
			owner.teamComputer.getPlayer(i).AI.do_quarterend();
		}
		if (!Instance<Ball>.instance.isShooting)
		{
			Instance<Ball>.instance.setVelocityZero();
			Instance<Ball>.instance.do_idle();
		}
		if (owner.quarter_num >= 5)
		{
			Instance<AudioController>.instance.play(AudioType.Sound_timeup);
		}
		owner.isShowedQuarterEnd = false;
	}

	public override void update(Court owner, float deltaTime)
	{
		base.update(owner, deltaTime);
		if (!Instance<Ball>.instance.isShooting)
		{
			Instance<Ball>.instance.do_idle();
		}
		if (!Instance<Ball>.instance.isShooting)
		{
			Vector3 position = Instance<Ball>.instance.getPosition();
			if (position.y < 0.4f)
			{
				if (owner.quarter_num >= 5 && owner.teamUser.Score != owner.teamComputer.Score)
				{
					owner.do_over();
				}
				else if (!owner.isShowedQuarterEnd)
				{
					Instance<UIGame>.instance.show_quaterend();
					owner.isShowedQuarterEnd = true;
				}
			}
		}
		if (owner.quarter_num < 5 || owner.teamUser.Score == owner.teamComputer.Score)
		{
			if (Global.isAndroid)
			{
				if (UnityEngine.Input.touchCount > 0 && Instance<UIGame>.instance.currentWidget == Instance<UIGame>.instance.widgetQuarterEnd && owner.stateMachine.timeInState > 1.5f && !Platform.isFullScreenShowing())
				{
					startNextQuarter(owner);
				}
			}
			else if (Input.GetMouseButtonDown(0) && Instance<UIGame>.instance.currentWidget == Instance<UIGame>.instance.widgetQuarterEnd && owner.stateMachine.timeInState > 1.5f)
			{
				startNextQuarter(owner);
			}
		}
		if (owner.stateMachine.timeInState > 10f)
		{
			Vector3 position2 = Instance<Ball>.instance.getPosition();
			if (position2.y > 0.4f)
			{
				Instance<Ball>.instance.setPositon(MathHelper.setY(Instance<Ball>.instance.getPosition(), 0.1f));
			}
		}
	}

	public override void exit(Court owner)
	{
		base.exit(owner);
		if (owner.quarter_num >= 5)
		{
			owner.OTCount++;
		}
		owner.resetQuarterTime();
		owner.teamUser.setIsAllAI(_isAllAI: false);
		owner.teamUser.player_control.do_idle();
	}

	private void startNextQuarter(Court owner)
	{
		Instance<UIGame>.instance.isCountingDown = false;
		Instance<UIGame>.instance.show_playing();
		if ((owner.isUserGetJumpBall && owner.quarter_num % 2 == 0) || (!owner.isUserGetJumpBall && owner.quarter_num % 2 == 1))
		{
			Instance<Ball>.instance.pos_Out = ((owner.teamComputer.offenseDirection != 0) ? Global.posBaseLineLeft : Global.posBaseLineRight);
			Instance<Ball>.instance.pos_Out = MathHelper.setY(Instance<Ball>.instance.pos_Out, 0f);
			owner.do_foul(FoulType.OUTSIDE, TeamType.USER);
			int num = 0;
			while (true)
			{
				if (num < Team.PlayerCount)
				{
					if (owner.teamComputer.getPlayer(num).AI.isInState(PlayerAIState_BaseLine.getInstance()))
					{
						break;
					}
					num++;
					continue;
				}
				return;
			}
			owner.teamComputer.getPlayer(num).setPositon(owner.teamComputer.getPlayer(num).bassline_targetPos);
			owner.teamComputer.getPlayer(num).getBallInHand();
			owner.teamComputer.getPlayer(num).do_dribble_run();
			owner.teamComputer.getPlayer(num).LookAt(owner.teamComputer.getPlayer_AcceptBaseLine().getPosition());
			if (owner.teamComputer.offenseDirection == OffenseDirection.RIGHT)
			{
				Instance<CameraController>.instance.cameraMain.transform.rotation = Quaternion.Euler(MathHelper.setY(Instance<CameraController>.instance.cameraMain.transform.eulerAngles, 60f));
				Instance<CameraController>.instance.cameraMain.fieldOfView = 30f;
				Instance<CameraController>.instance.cameraMain.transform.localPosition = Global.cameraPos_Left;
			}
			else
			{
				Instance<CameraController>.instance.cameraMain.transform.rotation = Quaternion.Euler(MathHelper.setY(Instance<CameraController>.instance.cameraMain.transform.eulerAngles, 120f));
				Instance<CameraController>.instance.cameraMain.fieldOfView = 30f;
				Instance<CameraController>.instance.cameraMain.transform.localPosition = Global.cameraPos_Right;
			}
			return;
		}
		Instance<Ball>.instance.pos_Out = ((owner.teamUser.offenseDirection != 0) ? Global.posBaseLineLeft : Global.posBaseLineRight);
		Instance<Ball>.instance.pos_Out = MathHelper.setY(Instance<Ball>.instance.pos_Out, 0f);
		owner.do_foul(FoulType.OUTSIDE, TeamType.COMPUTER);
		int num2 = 0;
		while (true)
		{
			if (num2 < Team.PlayerCount)
			{
				if (owner.teamUser.getPlayer(num2).AI.isInState(PlayerAIState_BaseLine.getInstance()))
				{
					break;
				}
				num2++;
				continue;
			}
			return;
		}
		owner.teamUser.getPlayer(num2).setPositon(owner.teamUser.getPlayer(num2).bassline_targetPos);
		owner.teamUser.getPlayer(num2).getBallInHand();
		owner.teamUser.getPlayer(num2).do_dribble_run();
		owner.teamUser.getPlayer(num2).LookAt(owner.teamUser.getPlayer_AcceptBaseLine().getPosition());
		if (owner.teamUser.offenseDirection == OffenseDirection.RIGHT)
		{
			Instance<CameraController>.instance.cameraMain.transform.rotation = Quaternion.Euler(MathHelper.setY(Instance<CameraController>.instance.cameraMain.transform.eulerAngles, 60f));
			Instance<CameraController>.instance.cameraMain.fieldOfView = 30f;
			Instance<CameraController>.instance.cameraMain.transform.localPosition = Global.cameraPos_Left;
		}
		else
		{
			Instance<CameraController>.instance.cameraMain.transform.rotation = Quaternion.Euler(MathHelper.setY(Instance<CameraController>.instance.cameraMain.transform.eulerAngles, 120f));
			Instance<CameraController>.instance.cameraMain.fieldOfView = 30f;
			Instance<CameraController>.instance.cameraMain.transform.localPosition = Global.cameraPos_Right;
		}
	}
}
